<template>
  <full-dialog :visible.sync="visible" :fullscreen.sync="fullscreen" :title="title" width="80%" :close-on-click-modal="false" @closed="reset">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-divider content-position="left">流程</el-divider>

      <el-form-item class="flow-form-item" prop="flowNodes">
        <flow-create v-model="form.flowNodes" :edit="true" :table="form.mainTable"></flow-create>
      </el-form-item>

      <el-divider content-position="left" style="margin-top: 10px">模板信息</el-divider>
      <el-row>
        <el-col :span="12" :xs="24">
          <el-form-item label="流程编号" prop="code">
            <el-input v-model="form.code" placeholder="请输入流程编号"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" :xs="24">
          <el-form-item label="流程名" prop="name">
            <el-input v-model="form.name" placeholder="请输入流程名"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12" :xs="24">
          <el-form-item label="主表" prop="mainTable">
            <el-select
              v-model="form.mainTable"
              placeholder="请选择主表"
              filterable
              size="small"
              style="width: 100%">
              <el-option
                v-for="item in tablesOptions"
                :key="item"
                :label="item"
                :value="item"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" :xs="24">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue">
                {{ dict.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <!--<el-row>
        <el-col :span="12" :xs="24">
          <el-form-item label="服务类" prop="flowService">
            <el-input v-model="form.flowService" placeholder="请输入服务类"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" :xs="24">
          <el-form-item label="表单地址" prop="flowView">
            <el-input v-model="form.flowView" placeholder="请输入表单地址"/>
          </el-form-item>
        </el-col>
      </el-row>-->
      <el-row>
        <el-col :span="12" :xs="24">
          <el-form-item label="产业" prop="corpIds">
            <el-cascader
              ref="corpCascader"
              v-model="form.corpIds"
              :options="corpOptions"
              :props="corpProps"
              :show-all-levels="false"
              placeholder="请选择产业"
              style="width: 100%">
            </el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="submitting" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </full-dialog>
</template>

<script>
import { flowTemplateInfo, addFlowTemplate, updateFlowTemplate, allTables } from "@/api/system/flowTemplate";
import { corpTree } from "@/api/system/corp";

export default {
  name: "Edit",
  props: {
    statusOptions: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      id: undefined,
      visible: false,
      fullscreen: true,
      title: "流程模板",
      submitting: false,
      tablesOptions: [],
      corpOptions: [],
      form: {
        flowNodes: [],
        code: undefined,
        name: undefined,
        mainTable: undefined,
        status: '0',
        flowService: undefined,
        flowView: undefined,
        corpIds: [],
        corpId: undefined,
        corp: undefined
      },
      rules: {
        flowNodes: [
          { required: true, message: "请至少添加一个审批节点", trigger: "change", type: 'array' }
        ],
        code: [
          { required: true, message: "流程编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "流程名不能为空", trigger: "blur" }
        ],
        mainTable: [
          { required: true, message: "主表不能为空", trigger: "change" }
        ],
        flowService: [
          { required: true, message: "服务类不能为空", trigger: "blur" }
        ],
        flowView: [
          { required: true, message: "表单地址不能为空", trigger: "blur" }
        ],
        corpIds: [
          {required: true, message: "产业不能为空", trigger: "change", type: "array"}
        ]
      },
      corpProps: {
        value: 'id',
        label: 'label'
      }
    }
  },
  methods: {
    init(id, title) {
      this.id = id;
      this.title = title;
      this.visible = true;
      this.initTables();
      if (id) {
        this.initData();
      }
    },
    initTables() {
      corpTree().then(res => {
        this.corpOptions = res;
        this.corpOptions.splice(0, 0, {
          id: "0",
          label: "通用"
        });
      });
      allTables().then(res => {
        this.tablesOptions = res;
      });
    },
    initData() {
      flowTemplateInfo(this.id).then(res => {
        this.form = res;
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const corpNodes = this.$refs.corpCascader.getCheckedNodes();
          const corpNode = corpNodes[corpNodes.length - 1];
          this.form.corpId = corpNode.value;
          this.form.corp = corpNode.label;

          this.submitting = true;
          if (this.id) {
            updateFlowTemplate(this.id, this.form).then(response => {
              this.submitting = false;

              this.msgSuccess("修改成功");
              this.close();
              this.$emit('success');
            }).catch(() => {
              this.submitting = false;
            });
          } else {
            addFlowTemplate(this.form).then(response => {
              this.submitting = false;

              this.msgSuccess("新增成功");
              this.close();
              this.$emit('success');
            }).catch(() => {
              this.submitting = false;
            });
          }
        }
      });
    },
    close() {
      this.visible = false;
    },
    // 表单重置
    reset() {
      this.id = undefined;
      this.form = {
        flowNodes: [],
        code: undefined,
        name: undefined,
        mainTable: undefined,
        status: '0',
        flowService: undefined,
        flowView: undefined,
        corpIds: [],
        corpId: undefined,
        corp: undefined
      };
      this.resetForm("form");
    }
  }
}
</script>

<style scoped lang="scss">

</style>
